<template>
	<div v-if="isVisible" class="custom-loading">
		<div class="loading-overlay" @click.stop></div>
		<div class="loading-content">
			<div class="loading-spinner"></div>
			<div class="loading-text">{{ loadingText }}</div>
			<div class="close-button" @click="closeLoading">×</div>
		</div>
	</div>
</template>

<script lang="ts">
	import { defineComponent, ref } from 'vue';

	export default defineComponent({
		name: 'Loading',
		setup(_, { emit }) {
			const isVisible = ref(false);
			const loadingText = ref('加载中...'); // 默认加载文本

			// 关闭加载框
			const closeLoading = () => {
				isVisible.value = false; // 隐藏加载框
				emit('close'); // 触发关闭事件，供外部使用
			};

			// 显示加载框
			const showLoading = (text: string) => {
				loadingText.value = text;
				isVisible.value = true; // 显示加载框
				emit("show");
			};

			return {
				isVisible,
				closeLoading,
				loadingText,
				showLoading
			};
		}
	});
</script>

<style scoped>
	.custom-loading {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 9999;
	}

	.loading-overlay {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6); /* 半透明背景 */
	}

	.loading-content {
		background-color: #fff;
		width: 250px;
		padding: 30px;
		border-radius: 12px;
		text-align: center;
		position: relative;
		box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
		z-index: 10000;
	}

	.loading-spinner {
		width: 50px;
		height: 50px;
		border: 4px solid rgba(0, 0, 0, 0.1);
		border-top-color: #3498db;
		border-radius: 50%;
		animation: spin 1s linear infinite;
		margin: 0 auto 20px;
	}

	@keyframes spin {
		0% {
			transform: rotate(0deg);
		}
		100% {
			transform: rotate(360deg);
		}
	}

	.loading-text {
		color: #333;
		font-size: 16px;
		font-weight: 500;
		margin-top: 10px;
	}

	.close-button {
		position: absolute;
		top: 8px;
		right: 8px;
		font-size: 18px;
		color: #888;
		cursor: pointer;
		transition: color 0.3s;
	}

	.close-button:hover {
		color: #3498db;
	}
</style>
